package com.jzo2o.foundations.handler;

import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * xxl-job 定时任务 定时更新缓存
 */
@Component
@Slf4j
public class SpringCacheSyncHandler {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IRegionService iRegionService;
    @Autowired
    private IServeService iServeService;

    //定时更新缓存 开通区域列表和首页服务列表
    @XxlJob("activeRegionCacheSync")
    public void regionAndServeListSync() {
        log.info("========开始同步开通区域列表数据=============");
        //1、先删除缓存中开通区域列表的数据
        redisTemplate.delete("JZ_CACHE::ACTIVE_REGIONS");
        //2、查询开通的区域列表，并放入缓存
        List<RegionSimpleResDTO> regionSimpleResDTOS = iRegionService.queryActiveRegionListCache();
        log.info("========开始同步首页服务列表数据=============");
        for (RegionSimpleResDTO dto : regionSimpleResDTOS) {
            Long id = dto.getId();
            //3、先删除缓存中首页服务列表的数据
            redisTemplate.delete(RedisConstants.CacheName.SERVE_ICON + "::" + id);
            //4、在查询首页服务列表数据，并将其放入缓存
            iServeService.fiandServeList(id);
            //5、先删除缓存中热门服务列表的数据
            redisTemplate.delete(RedisConstants.CacheName.HOT_SERVE + "::" + id);
            //6、在查询热门服务列表的数据，并将其放入缓存
            iServeService.findHotServeList(id);
        }

    }
}
